﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Invaliditet typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class InvaliditetDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<InvaliditetDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Invaliditet _currentInvaliditet = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxInvaliditetDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxInvaliditetErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxInvaliditetBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDOsoba property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDOsobaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Stupanj property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxStupanjDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the DatumOd property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDatumOdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the DatumDo property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDatumDoDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Osoba> _IDOsobaList;
		
		/// <summary> 
		/// The list of selectable Osoba
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Osoba> IDOsobaList
		{
			get {return this._IDOsobaList;}
			set 
			{
				this._IDOsobaList = value;
				this.uxIDOsobaDataGridViewColumn.DataSource = null;
				this.uxIDOsobaDataGridViewColumn.DataSource = this._IDOsobaList;
			}
		}
		
		private bool _allowNewItemInIDOsobaList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Osoba
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDOsobaList
		{
			get { return _allowNewItemInIDOsobaList;}
			set
			{
				this._allowNewItemInIDOsobaList = value;
				this.uxIDOsobaDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Invaliditet> _InvaliditetList;
				
		/// <summary> 
		/// The list of Invaliditet to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Invaliditet> InvaliditetList
		{
			get {return this._InvaliditetList;}
			set
			{
				this._InvaliditetList = value;
				this.uxInvaliditetBindingSource.DataSource = null;
				this.uxInvaliditetBindingSource.DataSource = value;
				this.uxInvaliditetDataGridView.DataSource = null;
				this.uxInvaliditetDataGridView.DataSource = this.uxInvaliditetBindingSource;				
				//this.uxInvaliditetBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxInvaliditetBindingSource_ListChanged);
				this.uxInvaliditetBindingSource.CurrentItemChanged += new System.EventHandler(OnInvaliditetBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnInvaliditetBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentInvaliditet = uxInvaliditetBindingSource.Current as Entities.Invaliditet;
			
			if (_currentInvaliditet != null)
			{
				_currentInvaliditet.Validate();
			}
			//_Invaliditet.Validate();
			OnCurrentEntityChanged();
		}

		//void uxInvaliditetBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Invaliditet"/> instance.
		/// </summary>
		public Entities.Invaliditet SelectedInvaliditet
		{
			get {return this._currentInvaliditet;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxInvaliditetDataGridView.VirtualMode;}
			set
			{
				this.uxInvaliditetDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxInvaliditetDataGridView.AllowUserToAddRows;}
			set {this.uxInvaliditetDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxInvaliditetDataGridView.AllowUserToDeleteRows;}
			set {this.uxInvaliditetDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxInvaliditetDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxInvaliditetDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="InvaliditetDataGridViewBase"/> class.
		/// </summary>
		public InvaliditetDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxInvaliditetDataGridView = new System.Windows.Forms.DataGridView();
			this.uxInvaliditetBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxInvaliditetErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDOsobaDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxStupanjDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDatumOdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDatumDoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDOsobaBindingSource = new OsobaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDOsobaBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxInvaliditetErrorProvider
			// 
			this.uxInvaliditetErrorProvider.ContainerControl = this;
			this.uxInvaliditetErrorProvider.DataSource = this.uxInvaliditetBindingSource;						
			// 
			// uxInvaliditetDataGridView
			// 
			this.uxInvaliditetDataGridView.AutoGenerateColumns = false;
			this.uxInvaliditetDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxInvaliditetDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDOsobaDataGridViewColumn,
		this.uxStupanjDataGridViewColumn,
		this.uxDatumOdDataGridViewColumn,
		this.uxDatumDoDataGridViewColumn			});
			this.uxInvaliditetDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxInvaliditetDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxInvaliditetDataGridView.Name = "uxInvaliditetDataGridView";
			this.uxInvaliditetDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxInvaliditetDataGridView.TabIndex = 0;	
			this.uxInvaliditetDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxInvaliditetDataGridView.EnableHeadersVisualStyles = false;
			this.uxInvaliditetDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnInvaliditetDataGridViewDataError);
			this.uxInvaliditetDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnInvaliditetDataGridViewCellValueNeeded);
			this.uxInvaliditetDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnInvaliditetDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDOsobaDataGridViewColumn
			//
			this.uxIDOsobaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDOsobaDataGridViewColumn.DataPropertyName = "IDOsoba";
			this.uxIDOsobaDataGridViewColumn.HeaderText = "IDOsoba";
			this.uxIDOsobaDataGridViewColumn.Name = "uxIDOsobaDataGridViewColumn";
			this.uxIDOsobaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDOsobaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDOsobaDataGridViewColumn.ReadOnly = false;		
			//
			// uxStupanjDataGridViewColumn
			//
			this.uxStupanjDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxStupanjDataGridViewColumn.DataPropertyName = "Stupanj";
			this.uxStupanjDataGridViewColumn.HeaderText = "Stupanj";
			this.uxStupanjDataGridViewColumn.Name = "uxStupanjDataGridViewColumn";
			this.uxStupanjDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxStupanjDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxStupanjDataGridViewColumn.ReadOnly = false;		
			//
			// uxDatumOdDataGridViewColumn
			//
			this.uxDatumOdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDatumOdDataGridViewColumn.DataPropertyName = "DatumOd";
			this.uxDatumOdDataGridViewColumn.HeaderText = "DatumOd";
			this.uxDatumOdDataGridViewColumn.Name = "uxDatumOdDataGridViewColumn";
			this.uxDatumOdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDatumOdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDatumOdDataGridViewColumn.ReadOnly = false;		
			//
			// uxDatumDoDataGridViewColumn
			//
			this.uxDatumDoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDatumDoDataGridViewColumn.DataPropertyName = "DatumDo";
			this.uxDatumDoDataGridViewColumn.HeaderText = "DatumDo";
			this.uxDatumDoDataGridViewColumn.Name = "uxDatumDoDataGridViewColumn";
			this.uxDatumDoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDatumDoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDatumDoDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDOsobaDataGridViewColumn
			//				
			this.uxIDOsobaDataGridViewColumn.DisplayMember = "Ime";	
			this.uxIDOsobaDataGridViewColumn.ValueMember = "ID";	
			this.uxIDOsobaDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDOsobaDataGridViewColumn.DataSource = uxIDOsobaBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxInvaliditetDataGridView);
			this.Name = "InvaliditetDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDOsobaBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxInvaliditetBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				InvaliditetDataGridViewEventArgs args = new InvaliditetDataGridViewEventArgs();
				args.Invaliditet = _currentInvaliditet;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class InvaliditetDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Invaliditet	_Invaliditet;
	
			/// <summary>
			/// the  Entities.Invaliditet instance.
			/// </summary>
			public Entities.Invaliditet Invaliditet
			{
				get { return _Invaliditet; }
				set { _Invaliditet = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxInvaliditetDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnInvaliditetDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxInvaliditetDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnInvaliditetDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxInvaliditetDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = InvaliditetList[e.RowIndex].ID;
						break;
					case "uxIDOsobaDataGridViewColumn":
						e.Value = InvaliditetList[e.RowIndex].IDOsoba;
						break;
					case "uxStupanjDataGridViewColumn":
						e.Value = InvaliditetList[e.RowIndex].Stupanj;
						break;
					case "uxDatumOdDataGridViewColumn":
						e.Value = InvaliditetList[e.RowIndex].DatumOd;
						break;
					case "uxDatumDoDataGridViewColumn":
						e.Value = InvaliditetList[e.RowIndex].DatumDo;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxInvaliditetDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnInvaliditetDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxInvaliditetDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						InvaliditetList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDOsobaDataGridViewColumn":
						InvaliditetList[e.RowIndex].IDOsoba = (System.Int32)e.Value;
						break;
					case "uxStupanjDataGridViewColumn":
						InvaliditetList[e.RowIndex].Stupanj = (System.String)e.Value;
						break;
					case "uxDatumOdDataGridViewColumn":
						InvaliditetList[e.RowIndex].DatumOd = (System.DateTime)e.Value;
						break;
					case "uxDatumDoDataGridViewColumn":
						InvaliditetList[e.RowIndex].DatumDo = (System.DateTime?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
